﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using compshop.Infrastructure;
using compshop.Infrastructure.CompShopDataService;
namespace compshop.Modules.ListOfGoodsInWaybill.Presenter
{
  public  class GoodsListInWaybillPresenter
    {
        private CompShopEntities compShopDataContext;
        public IViewGoodsListInWaybill View { set; get; }
        private EventsManager eventsManager;
        public GoodsListInWaybillPresenter( EventsManager eventsManager)
        {
            compShopDataContext= CompShopEntitiesContext.GetInstance();
            EmployeeList = compShopDataContext.Employees.ToList();
            DepartmentList = compShopDataContext.Departments.ToList();
            this.eventsManager = eventsManager;
            eventsManager.Presenter = this;
            eventsManager.Subscribe();

            
        }

        public Waybill waybill { set; get; }
        public List<Department> DepartmentList { set; get; }
        public List<Employee> EmployeeList { set; get; }

        public void save(Waybill changes)
        {
            waybill.ReceiverID = changes.ReceiverID;
            waybill.SenderID = changes.SenderID;
            waybill.ReceivingDate = changes.ReceivingDate;
            waybill.SendingDate = changes.SendingDate;
        }
       #region Publishers

       //public void EmployeeSelectedPublisher(int id)
       //{
       //  eventsManager.eventAggregator.GetEvent<EmployeeSelectedEvent>().Publish(id);
       //}

       #endregion

       #region Handlers

       public void WayBillSelectedEventHandler(int id)
        {
            waybill = compShopDataContext.Waybills.Where(x => x.WaybillID == id).FirstOrDefault();
           
            waybill.RequestForDeliveries.Load(compShopDataContext.RequestForDeliveries.Expand("Product").Where(x=>x.WaybillID==waybill.WaybillID));
            foreach (RequestForDelivery p in waybill.RequestForDeliveries)
            {
                compShopDataContext.LoadProperty(p.Product, "GeneralProduct");
                compShopDataContext.LoadProperty(p.Product.GeneralProduct, "SubCategory");
            }
            View.refresh(this); 
        }

       #endregion

    }
}
